package com.tuanzi.loan.web.bean.businesssystem;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.businesssystem.BusinessMain;
import com.tuanzi.loan.business.entity.businesssystem.BusinessPrepay;
import com.tuanzi.loan.business.entity.system.SystemUser;
import com.tuanzi.loan.business.service.businesssystem.BusinessMainService;
import com.tuanzi.loan.business.service.businesssystem.BusinessPrepayService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.businesssystem.BusinessPrepayRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessPrepayResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.businesssystem.BusinessPrepayLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @author caiwanlin
 * @create 2018-08-22 17:48
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class BusinessPrepayListBean extends JSFListingBean<BusinessPrepayRequest, BusinessPrepayResponse> {
    @Autowired
    private BusinessMainService businessMainService;

    @Autowired
    private BusinessPrepayService settlementPrepayService;

    private List<BusinessMain> companyList;

    private List<BusinessPrepayResponse> selectPrepayList;

    private BusinessPrepay settlementPrepay;

    private BusinessMain businessMain;

    private Double allPrepayIncome = 0d;

    private List<SystemUser> checkList;//财务核查人

    @PostConstruct
    public void init() throws Exception {
        companyList = businessMainService.getCompanyList();
        settlementPrepay = new BusinessPrepay();
        initLazyDataModel();
    }

    private void initLazyDataModel() throws Exception {
        $ = new BusinessPrepayRequest();
        lazyDataModel = new BusinessPrepayLazyDataModel($);
    }

    public void onReset() throws Exception {
        initLazyDataModel();
    }

    public void count() {
        allPrepayIncome = settlementPrepayService.getAllPrepayIncome($);
    }

    public void onAdd() {
        settlementPrepay = new BusinessPrepay();
    }

    public void onSee(Long id) {
        settlementPrepay = settlementPrepayService.findOne(id);
    }

    public void onStatus(Long id){
        BusinessPrepay one = settlementPrepayService.findOne(id);
        if (one.getBillStatus()!=null&&one.getBillStatus()==true){
            addWarnMessage("已经签收");
            return;
        }
        one.setBillStatus(true);
        settlementPrepayService.saveOrUpdate(one);

    }

    public void onSave() {
        if (!validate()) return;
        if (settlementPrepay.getBillStatus()!=null&&settlementPrepay.getBillStatus()==true){
            addWarnMessage("已经签收，不支持更改！");
            return;
        }
        //获取公司名称
        settlementPrepayService.onSave(settlementPrepay);
        execute("PF('adding').hide();");
        update("prepayForm");
    }


    public void onUploadPayProof(FileUploadEvent event) {
        settlementPrepay.setPayProof(this.uploadImg(event.getFile()));
    }

    public void onDelete(Long id) throws Exception {
        BusinessPrepay one = settlementPrepayService.findOne(id);
        if (one.getBillStatus()!=null&&one.getBillStatus()==true){
            addWarnMessage("不能删除已经签收的发票");
            return;
        }
        try {
            settlementPrepayService.delete(one);
            businessMainService.prepayDelete(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        addSuccessfullyMessage();
    }

    public boolean validate() {
        if (settlementPrepay.getCompanyId() == null) {
            addErrorMessage("没有选择主体");
            return false;
        }
        if (settlementPrepay.getPrepayTime() == null) {
            addErrorMessage("没有选择付款时间");
            return false;
        }
        if (settlementPrepay.getPrepayAmount() == null) {
            addErrorMessage("付款金额为空");
            return false;
        }
        if (settlementPrepay.getPayMode() == null) {
            addErrorMessage("没有选择付款方式");
            return false;
        }
        if (settlementPrepay.getPayMode() == null) {
            addErrorMessage("没有选择付款类别");
            return false;
        }
        return true;
    }


    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(SETTLEMENT_PAY_MODE, BUSINESS_MAIN_TYPE,BUSINESS_PREPAY_TYPE);
    }
}
